<?php
class MON{
	private static $_connect;
	
	private static $table_prefix;
	
	private $_conn;
	
	private $error;
	
	private $model;
	
	private $_id;
	
	public $result = false;
	
	private static $inser_id = null;
	
	private static function table_prefix(){
		return (string)self::$table_prefix;
	}
	
	public static function connect(array $arr_config = array()){
		if (isset($arr_config['username']) && isset($arr_config['password'])){
			self::$_connect = new mongo($arr_config['host'],array('username'=>$arr_config['username'],'password'=>$arr_config['password']));
		}else{
			self::$_connect = new mongo($arr_config['host']);
		}
		
		if (isset($arr_config['table_prefix']))
			self::$table_prefix = (string)$arr_config['table_prefix'];
		
        self::$_connect = self::$_connect->selectDB($arr_config['dbname']);
        
        return self::$_connect;
	}
	
	private static function instance($model = null,$mongoId = null){
		
		$_instance = new self;
		if (!is_object(self::$_connect)){
			return $_instance->error('数据库尚未初始化');
		}
		
		if (!is_string($model)){
			return $_instance->error('模型字符串不正确');
		}else{
			$_instance->model = $model;
		}
		$_instance->_conn = self::$_connect->selectCollection($model);
		
		if(strlen($mongoId = (string)$mongoId)){
			$_instance->_id = $mongoId;
			return $_instance->findOne(array('_id' => new MongoId($mongoId)));
		}
		
		return $_instance;
	}
	
	public static function factory($model = null,$mongoId = null){
		$model = self::table_prefix().$model;
		return self::instance($model,$mongoId);
	}
	
	public function error($message = null){
		if (is_string($message) && !empty($message)){
			$this->error = $message;
			if (!headers_sent()){
				header('content-type:text/html;charset=utf-8');
			}
			echo '<p>error::'.$this->error."</p>\n";
			return $this;
		}
		return $this->error;
	}
	
	public function find(array $query = array(),array $fields = array()){
		/*$result = $this->_conn->find($query,$fields);
		if (is_object($result)){
			$this->result = array();
			while ($tmp = $result->getNext()) {
				$this->result[] = $tmp;
			}
		}*/
		
		return $this->result = $this->_conn->find($query,$fields);
	}
	
	public static function as_array($MongoCursor){
		if (is_object($MongoCursor)){
			$result = array();
			while ($tmp = $MongoCursor->getNext()) {
				$result[] = $tmp;
			}
			return $result;
		}
		return $MongoCursor;
	}
	
	public function findOne(array $query = array(),array $fields = array()){
		return $this->result = $this->_conn->findOne($query,$fields);
	}
	
	public function insert(array $a,array $options = array()){
		$result = $this->_conn->insert($a,$options);
		if ($result) self::$inser_id = $a['_id'];
		return $result;
	}
	
	public function loaded(){
		return (bool)($this->result);
	}
	
	public static function lastError(){
		return self::$_connect->lastError();
	}
	
	public function __call($name, $arguments){
		return call_user_func_array(array($this->_conn,$name),$arguments);
	}
	
	public static function inser_id(){
		return self::$inser_id;
	}
}